import paramiko

#SSH批量执行命令
#hosts = [] #需要提前拥有一个IP数组传入ssh-exec

def ssh_exec(host, user, key_path, command):
    try:
        client = paramiko.SSHClient()
        client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        client.connect(host, username=user, key_filename=key_path)
        stdin, stdout, stderr = client.exec_command(command)
        result = stdout.read().decode().strip()
        error = stderr.read().decode().strip()
        client.close()
        if error:
            return {"status": "error", "host": host, "message": error}
        return {"status": "success", "host": host, "output": result}
    except Exception as e:
        return {"status": "error", "host": host, "message": str(e)}

